package com.wthink.hzgj.copyFile

import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.Navigation
import androidx.navigation.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.wthink.hzgj.BusApplication
import com.wthink.hzgj.R
import com.wthink.hzgj.copyFile.adapter.CameraListAdapter
import com.wthink.hzgj.copyFile.callback.DirectoryIsEmpty
import com.wthink.hzgj.copyFile.viewmodel.CopyFileCameraViewModel
import com.wthink.hzgj.data.domain.CameraFileInfo
import com.wthink.hzgj.data.domain.FileDetail
import com.wthink.hzgj.data.domain.NoticeMsg
import com.wthink.hzgj.databinding.FragmentCopyFileCareraBinding
import com.wthink.hzgj.msgList.adapter.MsgAdapter
import com.wthink.hzgj.util.ClickUtil
import com.wthink.hzgj.util.`interface`.ProgrossCallBack
import com.wthink.hzgj.util.adapter.OnItemClickListener
import com.wthink.hzgj.util.closeDefaultAnimator
import com.wthink.hzgj.util.toast


class CopyFileCameraFragment :Fragment(){

    private val viewModel by viewModels<CopyFileCameraViewModel>()

    private lateinit var binding: FragmentCopyFileCareraBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_copy_file_carera,
            container,
            false
        )
        binding.viewModel = viewModel
        binding.lifecycleOwner = this

        return binding.root
    }

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        viewModel.query()

        binding.back.setOnClickListener {
            it.findNavController().popBackStack()
        }


        binding.cameraListView.layoutManager = LinearLayoutManager(context,
            LinearLayoutManager.VERTICAL,false)

        binding.cameraListView.adapter = CameraListAdapter().apply {

            mItemClickListener = object : OnItemClickListener {
                override fun onItemClick(obj: Any, position: Int) {
                    val fileDetail = obj as FileDetail
                    viewModel.setdirectoryIsEmpty(object : DirectoryIsEmpty {
                        override fun fileSize(size: Int) {
                            if (size > 0) {
                                val args = Bundle()
                                args.putString("path",fileDetail.path)
                                Navigation.findNavController(binding.cameraListView).navigate(R.id.copyFileDirectoryFragment,args)
                            }
                        }
                    })

                    viewModel.queryDetailCheck(fileDetail.path)


/*
                    viewModel.queryDetailCheck(fileDetail.path).let {
                        if (it.isEmpty() || it.size == 0) {
                            return
                        }
                        val args = Bundle()
                        args.putString("path",fileDetail.path)
                        Navigation.findNavController(binding.cameraListView).navigate(R.id.copyFileDirectoryFragment,args)
                    }
*/
                }
            }
        }
        closeDefaultAnimator(binding.cameraListView)
    }
}